import dayjs from 'dayjs';
import numeral from 'numeral';

const convertPrice = (price: any) => {
  if (!price) {
    return;
  }

  if (typeof price === 'number') {
    return numeral(price / 100).format('0.00');
  }

  return price;
};

export const formatStock = (stock: any) => {
  return {
    B5P: convertPrice(stock?.f11), // 买5价格
    B5V: stock?.f12, // 买5量
    B4P: convertPrice(stock?.f13),
    B4V: stock?.f14,
    B3P: convertPrice(stock?.f15),
    B3V: stock?.f16,
    B2P: convertPrice(stock?.f17),
    B2V: stock?.f18,
    B1P: convertPrice(stock?.f19),
    B1V: stock?.f20,

    S5P: convertPrice(stock?.f31), // 卖5价格
    S5V: stock?.f32, // 卖5量
    S4P: convertPrice(stock?.f33),
    S4V: stock?.f34,
    S3P: convertPrice(stock?.f35),
    S3V: stock?.f36,
    S2P: convertPrice(stock?.f37),
    S2V: stock?.f38,
    S1P: convertPrice(stock?.f39),
    S1V: stock?.f40,

    newest: convertPrice(stock?.f43), // 最新价
    high: convertPrice(stock?.f44), // 最高价
    low: convertPrice(stock?.f45), // 最低价
    open: convertPrice(stock?.f46), // 开盘价
    volume: stock?.f47, // 成交量
    amount: stock?.f48, // 成交额
    outer: stock?.f49, // 外盘
    VR: stock?.f50, // 量比
    prevClose: convertPrice(stock?.f60),
    LU: convertPrice(stock?.f51), // limit up 涨停
    LD: convertPrice(stock?.f52), // Limit down 跌停
    inner: stock?.f161, // 内盘
    time: stock?.f86 && dayjs(+stock?.f86 * 1000).format('YYYY-MM-DD HH:mm:ss'),
    Hms: stock?.f86 && dayjs(+stock?.f86 * 1000).format('HH:mm:ss'),
    chg: stock?.f170 && numeral(stock?.f170 / 100).format('0.00'), // 涨跌幅
    change: convertPrice(stock?.f169), // 涨跌额
    turn: convertPrice(stock?.f168), // 换手率
  };
};
